<?php

namespace Common\Model;
use Think\Model\RelationModel;
class OrgnizersModel extends RelationModel {
    /**
     * 自动验证
     * self::EXISTS_VALIDATE 或者0 存在字段就验证（默认）
     * self::MUST_VALIDATE 或者1 必须验证
     * self::VALUE_VALIDATE或者2 值不为空的时候验证
     */
    protected $_validate = array(
        array('orgnizer_name', 'require', '组织者名称不能为空！'),
        array('orgnizer_info', 'require', '组织者信息不能为空！'),
       array('orgnizer_name', '', '该组织名称已被占用', 0, 'unique', 1)
    );

    protected $_link = array(
        'Users'=>array(
            'mapping_type' => self::BELONGS_TO,
            'class_name'    =>'Users',
            'foreign_key' => 'uid'
        ),
    );

    /**
     * 输出组织者列表
     * @return array
     */
    public function showOrgnizersInfo()
    {
        $orgnizer = $this->relation(true)->select();

        $i = 0;
        $rs = [];
        foreach ($orgnizer as $o) {
            $rs[$i]['username'] = $o['Users']['username'];
            $rs[$i]['org_name'] = $o['orgnizer_name'];
            $rs[$i]['email'] = $o['Users']['email'];
            $rs[$i]['org_info'] = $o['orgnizer_info'];
            $rs[$i]['oid'] = $o['oid'];
            $i++;
        }
        return $rs;
    }

    /**
     * 输出对应oid的组织名
     * @param $oid
     * @return string
     */

    public function showOrgnizerName($oid)
    {
        $class_info = $this->where(['oid' => $oid])->select();
        header("Content-type: text/html; charset=utf-8");
        return $class_info[0]['orgnizer_name'];
    }

    /**
     * 更新对应oid的组织列表信息
     * @param $jsondata
     * @return bool
     */

    public function updateClassInfo($oid, $jsondata)
    {
        $this->where(['oid' => $oid])->save(['class_info' => $jsondata]);
        return True;
    }

    /**
     * 返回组织列表信息树形结构
     * @param $oid
     * @return mixed
     */

    public function getClassInfoTree($oid)
    {
        $orgnizers = $this;
        $class_info = $orgnizers->where(['oid' => $oid])->select();
        header("Content-type: text/html; charset=utf-8");
        //var_dump($class_info);

        return $class_info[0]['class_info'];// $class_info[0];
    }




}